# @FileName  : conf.py
# @Time      : 2022/3/5 17:18
# @Author    : LuZhaoHui
# @Software  : PyCharm

from configparser import ConfigParser


class MyParser(ConfigParser):
    "Inherit from built-in class: ConfigParser"

    def optionxform(self, optionstr):
        "Rewrite without lower()"
        return optionstr


class ocrConf():
    def __init__(self, file):
        self.initVar()
        self.config = MyParser()
        self.name = file
        self.readConf()

    def initVar(self):
        # 设计配置
        self.varConf = {'Title': '',
                        'Start': 0,
                        'CameraIndex': 0,
                        'CameraTime': 50,
                        'ImageResolution': 1,
                        'ImageInverse': 0,
                        'ColorSetup': 0,
                        'ImageVertical': 0,
                        'ImageHorizontal': 0,
                        'ImageRotate': 0,
                        'WindowStatus': 0}

        self.varMenu = {'ActionList': '',
                        'ActionType': '',
                        'ActionMark': ''}

    def getConfig(self, section, key):
        return self.config.get(section, key)

    def readConf(self):
        self.config.read(self.name, 'utf-8')
        # [CONF]
        for k in self.varConf:
            str = self.getConfig('CONF', k)
            if isinstance(self.varConf[k], int):
                self.varConf[k] = int(str)
            elif isinstance(self.varConf[k], float):
                self.varConf[k] = float(str)
            else:
                self.varConf[k] = str

        for k in self.varMenu:
            str = self.getConfig('MENU', k)
            self.varMenu[k] = str

    def writeConf(self, key, value):
        self.config.set('CONF', key, str(value))
        with open(self.name, 'w', encoding='utf-8') as f:
            self.config.write(f)
